package mn.idax.exchange.otc.service.impl;

import mn.idax.exchange.otc.enums.response.UserResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.FileUrlService;
import mn.idax.exchange.otc.util.LogUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: jack_huang
 * @Date: 2019/8/19 11:16
 */
@Service
public class FileUrlServiceImpl implements FileUrlService {

    @Value("${aliyun.fileAddressPrefix}")
    private String fileAddressPrefix;

    @Value("${aliyun.subFileLength}")
    private Integer subFileLength;

    @Override
    public String dealFileUrlAddress(String fileAddress) {
        return StringUtils.isEmpty(fileAddress) ? "" : fileAddressPrefix.concat(fileAddress);
    }

    @Override
    public String subFilePath(String fileAddress) {
//      String fileAddress = "http://otc-files.oss-ap-northeast-1.aliyuncs.com/20190819/5d2ae350042a447ca7598c47e1eac648.jpg";
        if (StringUtils.isEmpty(fileAddress) || fileAddress.length() < 57){
            LogUtils.info(this.getClass(),"fileAddress not correct， " + fileAddress);
            throw new IdaxException(UserResponseEnum.PIC_ADDRESS_ERROR.getRespCode());
        }
        return fileAddress.substring(subFileLength);
    }
}
